import { combineReducers } from 'redux';

/**
 * 初始化数据状态管理
 * @param {初始化数据} initialReducers
 */
export function createReducerManager(initialReducers) {
	const reducers = { ...initialReducers };
	let combinedReducer = combineReducers(reducers);

	return {
		getReducer: () => combinedReducer,
		reducer: (state: any, action: never) => combinedReducer(state, action),
		add: (key, reducer) => {
			if (!key || reducers[key]) {
				return;
			}
			reducers[key] = reducer;
			combinedReducer = combineReducers(reducers);
		},
		remove: (key) => {
			if (!key || !reducers[key]) {
				return;
			}
			delete reducers[key];
			combinedReducer = combineReducers(reducers);
		},
	};
}
